package com.wen.service.impl;

import com.wen.pojo.ProductType;
import com.wen.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.wen.mapper.ProductTypeMapper;
import com.wen.service.ProductTypeService;
@Service
public class ProductTypeServiceImpl implements ProductTypeService{

    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void redisDelType(){
        stringRedisTemplate.delete("productTypeTree");
    }
    //根据编号，名称查询
    @Override
    public Result findPCode(ProductType productType) {
        ProductType p = productTypeMapper.findPCode(productType);
        if(p == null){
            return Result.ok(200,"成功，不重复",true);
        }else{
            return Result.ok(200,"成功，不重复",false);
        }
    }

    //添加分类
    @Override
    public Result addType(ProductType productType) {
        ProductType p = productTypeMapper.findPName(productType.getTypeName());
        if(p != null){
            return Result.fail(1,"名称重复，失败");
        }
        int i  = productTypeMapper.addType(productType);
        if(i>0){
            redisDelType();
            return Result.ok(200,"成功",null);
        }else{
            return Result.fail(1,"失败");
        }
    }

    //删除分类
    @Override
    public Result delType(Integer typeId) {
        int i = productTypeMapper.delType(typeId);
        if(i>0){
            redisDelType();
            return Result.ok(200,"成功",null);
        }else{
            return Result.fail(1,"失败");
        }
    }

    //修改分类
    @Override
    public Result updType(ProductType productType) {
        int i = productTypeMapper.updType(productType);
        if(i>0){
            redisDelType();
            return Result.ok(200,"成功",null);
        }else{
            return Result.fail(1,"失败");
        }
    }
}
